<?php

class Sistema_ParametrosController extends App_Controller_Action
{

    //public  $db = '';
     
    public function init()
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        /* Initialize action controller here */
    }

    public function indexAction() {
        $obj = new Sistema_Model_DbTable_Parametros();
        $this->view->result = $obj->fetchAll();
    }
    
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ckeditor/ckeditor.js');
        $option = array('width'=>'70%');
        echo $this->view->CkeditorHelper('PAR_DADCHQ',$option);
        echo $this->view->CkeditorHelper('PAR_DADDEPCNT',$option);
        
        $form = new Sistema_Form_Parametros();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('PAR_ID');
                
                $data = array(
                    'PAR_ID'        => $form->getValue('PAR_ID'),
                    'PAR_TEL'       => $form->getValue('PAR_TEL'),
                    'PAR_END'       => $form->getValue('PAR_END'),
                    'PAR_EML'       => $form->getValue('PAR_EML'),
                    'PAR_EMLCNT'    => $form->getValue('PAR_EMLCNT'),
                    'PAR_FAC'       => $form->getValue('PAR_FAC'),
                    'PAR_TWI'       => $form->getValue('PAR_TWI'),
                    'PAR_INS'       => $form->getValue('PAR_INS'),
                    'PAR_DADCHQ'    => $form->getValue('PAR_DADCHQ'),
                    'PAR_DADDEPCNT' => $form->getValue('PAR_DADDEPCNT'),
                );
                
                $obj = new Sistema_Model_DbTable_Parametros();
                
                if ($id == 0){
                    $obj->insert($data);  
                }
                else{
                    $obj->update($data, 'PAR_ID = ' . $id);
                }
                
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
            
            
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Sistema_Model_DbTable_Parametros();
                $form->populate($obj->fetchRow('PAR_ID = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Sistema_Model_DbTable_Parametros();
            $ok = (bool) $obj->delete('PAR_ID = '.(int)$id);
            
            //$this->_helper->redirector('index'); 
        } else {
            //$id = $this->_getParam('id', 0);
            //$obj = new Sistema_Model_DbTable_Parametros();
            //$form->populate($obj->fetchRow('PAR_ID = ' . $id)->toArray());
        }
        
        echo json_encode(array('ok' => $ok));
    }

}

